package com.woniu.filter;

import com.woniu.utils.JwtUtils;
import org.springframework.cloud.gateway.filter.GatewayFilterChain;
import org.springframework.cloud.gateway.filter.GlobalFilter;
import org.springframework.http.HttpStatus;
import org.springframework.http.server.reactive.ServerHttpRequest;
import org.springframework.http.server.reactive.ServerHttpResponse;
import org.springframework.web.server.ServerWebExchange;
import reactor.core.publisher.Mono;

public class LoginFilter implements GlobalFilter {
    @Override
    public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
        ServerHttpRequest request = exchange.getRequest();
        ServerHttpResponse response = exchange.getResponse();
        try {
            String token = request.getHeaders().get("token").get(0);
            String username = JwtUtils.getAccount(token);
            if(username!=null){
                return chain.filter(exchange);
            }
        }catch (Exception e){

        }
        //fail
        response.setStatusCode(HttpStatus.UNAUTHORIZED);
        return response.setComplete();

    }
}
